package cn.chrisx.drad.dr;

import cn.chrisx.drad.core.DrAdConfig;
import cn.chrisx.drad.core.DrAdError;
import cn.chrisx.drad.wrap.Ad;
import cn.chrisx.drad.wrap.AdListener;

/**
 * @author Chris <jz_boy@163.com>
 * @version 1.0
 * @date 2018/11/21 18:46
 * @description
 * @copyright ©2018 www.chrisx.cn All rights reserved.
 */
public class DrListenerIntercept implements AdListener {

    private DrAdListener adListener;

    private DrRewardAdListener rewardAdListener;

    private DrAbstractAd drAd;

    private DrAdConfig currentConfig;

    public void setAdListener(DrAdListener adListener) {
        this.adListener = adListener;
    }

    public DrListenerIntercept(DrAdListener adListener, DrRewardAdListener rewardAdListener) {
        this.adListener = adListener;
        this.rewardAdListener = rewardAdListener;
    }

    public void setRewardAdListener(DrRewardAdListener rewardAdListener) {
        this.rewardAdListener = rewardAdListener;
    }

    void setDrAd(DrAbstractAd drAd) {
        this.drAd = drAd;
    }

    public void setCurrentConfig(DrAdConfig currentConfig) {
        this.currentConfig = currentConfig;
    }

    private void onAdLoadSuccess() {
        if (adListener != null) adListener.onAdLoadSuccess(drAd);
    }

    public void onAdLoadFailed(int errorCode) {
        if (adListener != null && drAd.isAlive()) adListener.onAdLoadFailed(drAd, errorCode);

    }

    private void onAdShow() {
        if (adListener != null && drAd.isAlive()) adListener.onAdShow(drAd);

    }

    private void onAdDismiss() {
        if (adListener != null && drAd.isAlive()) adListener.onAdDismiss(drAd);

    }

    private void onAdClicked() {
        if (adListener != null && drAd.isAlive()) adListener.onAdClicked(drAd);
    }

    public void onAdLoadingError(DrAdConfig config, int errorCode, DrAdError ex) {
        if (adListener != null && drAd.isAlive())
            adListener.onAdLoadingError(drAd, config, errorCode, ex);

    }

    private void onReward(boolean suc, Object reward) {
        if (adListener != null && drAd.isAlive()) rewardAdListener.onReward(drAd, suc, reward);
    }

    @Override
    public void onAdLoadSuccess(Ad ad) {

        drAd.setPlatform(ad.platform());

        onAdLoadSuccess();

        if (drAd.isAutoShow()) {
            drAd.show();
        }

    }

    @Override
    public void onAdLoadFailed(Ad ad, int code, DrAdError ex) {

        drAd.setPlatform(null);

        onAdLoadingError(currentConfig, code, ex);
        drAd.load();

//        switch (code) {
//            case DrAdError.ERROR_DEBUG:
//            case DrAdError.ERROR_VALID:
//            case DrAdError.ERROR_NOT_CONFIG_U:
//            case DrAdError.ERROR_NO_CONFIG:
//            case DrAdError.ERROR_NOT_SUPPORT:
//            case DrAdError.ERROR_REQUEST:
//                return;
//        }

    }

    @Override
    public void onAdShow(Ad ad) {
        onAdShow();
    }

    @Override
    public void onAdDismiss(Ad ad) {
        onAdDismiss();
    }

    @Override
    public void onAdClicked(Ad ad) {
        onAdClicked();
    }

    @Override
    public void onReward(Ad ad, boolean suc, Object reward) {
        onReward(suc, reward);
    }


}
